package com.punch.user.controller;

import com.punch.common.dto.user.teacher.TeacherAuditDTO;
import com.punch.common.dto.user.teacher.TeacherDTO;
import com.punch.common.dto.login.WXUserDTO;
import com.punch.common.model.LoginUser;
import com.punch.common.model.Result;
import com.punch.common.query.user.teacher.TeacherQueryInfo;
import com.punch.common.vo.user.teacher.SimpleTeacherVO;
import com.punch.common.vo.user.teacher.TeacherVO;
import com.punch.common.vo.user.user.SimpleSchoolVO;
import com.punch.user.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * 教师信息表
 *
 * @author tanchangguo
 * @email 17607195327@163.com
 * @date 2020-07-21 19:10:02
 */
@RestController
@Api(tags = "教师信息表管理")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 教师列表
     */
    @GetMapping("/teachers")
    @ApiOperation(value = "教师列表")
    public Result listTeacher(@ModelAttribute TeacherQueryInfo queryInfo) {
        return Result.succeed(teacherService.queryPage(queryInfo));
    }


    /**
     * 查看教师详情
     */
    @GetMapping("/teacher/{teacherId}")
    @ApiOperation(value = "查看教师详情", response = TeacherVO.class)
    public Result getTeacher(@PathVariable("teacherId") Long teacherId) {
        TeacherVO teacherVO = teacherService.getTeacher(teacherId);
        return Result.succeed(teacherVO);
    }

    /**
     * 新增教师信息
     */
    @PostMapping(value = "/teacher", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "新增教师信息", produces = "application/json;charset=UTF-8")
    public Result saveTeacher(@Valid @RequestBody TeacherDTO teacherDTO) {
        return Result.succeed(teacherService.saveTeacher(teacherDTO));
    }

    /**
     * 修改教师信息
     */
    @PutMapping("/teacher")
    @ApiOperation(value = "修改教师信息")
    public Result updateTeacher(@RequestBody TeacherDTO teacherDTO) {
        teacherService.updateTeacher(teacherDTO);
        return Result.succeed("修改成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/teacher/{teacherId}")
    @ApiOperation(value = "删除教师")
    public Result deleteTeacher(@PathVariable("teacherId") Long teacherId) {
        teacherService.deleteTeacher(teacherId);
        return Result.succeed("删除成功");
    }

    /**
     * 修改教师审核状态--由运营人员审核(管理员)
     */
    @PutMapping("/teacher/audit")
    @ApiOperation(value = "修改教师审核状态")
    public Result updateStatus(@Valid @RequestBody TeacherAuditDTO teacherAuditDTO) {
        teacherService.auditTeacher(teacherAuditDTO);
        return Result.succeed("修改成功");
    }


    /**
     * 教师启用/停用
     */
    @PutMapping("/teacher/enable/{teacherId}")
    @ApiOperation(value = "教师启用/停用")
    public Result enableTeacher(@PathVariable Long teacherId) {
        teacherService.enableTeacher(teacherId);
        return Result.succeed("修改成功");
    }

    /**
     * 教师下拉列表
     *
     * @return List<SimpleCourseVO>
     * @author xiaol
     * @since 2020/7/23
     */
    @GetMapping(value = "/teacher/simple", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "教师下拉列表", notes = "教师下拉列表", produces = "application/json")
    public Result<List<SimpleTeacherVO>> listTeacherSelect(@RequestParam(name = "teacherName", required = false) String teacherName,
                                                           @RequestParam(name = "teacherId", required = false) Long teacherId) {
        return Result.succeed(teacherService.listTeacherSelect(teacherName, teacherId));
    }

    /**
     * 根据openId获取教师信息
     *
     * @param openId openId
     */
    @GetMapping(value = "/teacher/openid/{openId}")
    @ApiOperation(value = "根据openId获取教师信息")
    public LoginUser getFeignTeacherByOpenId(@PathVariable String openId) {
        return teacherService.getFeignTeacherByOpenId(openId);
    }

    /**
     * 根据手机号码获取用户信息
     *
     * @param mobile 手机号码
     */
    @GetMapping(value = "/teacher/mobile/{mobile}")
    @ApiOperation(value = "根据手机号码获取用户信息")
    public LoginUser getFeignTeacherByMobile(@PathVariable String mobile) {
        return teacherService.getFeignTeacherByMobile(mobile);
    }


    /**
     * 进行注册,不存在则注册,存在则返回
     *
     * @param wxUserDTO 微信用户表单
     */
    @PostMapping(value = "/teacher/openid/mobile")
    public boolean saveFeignTeacher(@RequestBody WXUserDTO wxUserDTO) {
        return teacherService.saveFeignTeacher(wxUserDTO);
    }

    /**
     * 教师导入
     *
     * @param file 文件
     * @return string
     */
    @PostMapping(value = "/teacher/import", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "教师导入", notes = "教师导入", produces = "application/json")
    public Result importTeacher(@RequestParam("file") MultipartFile file) {
        teacherService.importTeacher(file);
        return Result.succeed("导入成功");
    }


    /**
     * 获取教师审批失败信息
     *
     * @param teacherId 教师id
     * @return string
     */
    @GetMapping("/teacher/{teacherId}/audit-message")
    @ApiOperation(value = "获取教师审批失败信息", response = SimpleSchoolVO.class)
    public Result getTeacherAuditMessage(@PathVariable Long teacherId) {
        return Result.succeed(teacherService.getTeacherAuditMessage(teacherId), "");
    }


    @GetMapping(value = "/teacher/handlerTeacherArea", produces = "application/json; charset=utf-8")
    public void handlerTeacherArea() {
        teacherService.handlerTeacherArea();
    }
}
